<template>
  <div>
    <el-dialog title="现场咨询" :visible.sync="modalShow" width="60%" :before-close="modalClose" center>
      <el-row type="flex">
        <el-row :span="12">
          <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
            <div class="top">
              <el-form-item label="主要需求" prop="oneDemand">
                <el-select v-model="ruleForm.oneDemand" placeholder="请选择" @change="changeValue(1,ruleForm.oneDemand)">
                  <el-option v-for="(item,i) of ruleForm.regions" :key="i" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="次需求1" prop="towDemand">
                <el-select v-model="ruleForm.towDemand" placeholder="请选择" @change="changeValue(2,ruleForm.towDemand)">
                  <el-option v-for="(item,i) of ruleForm.regions" :key="i" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="次需求2" prop="threeDemand">
                <el-select v-model="ruleForm.threeDemand" placeholder="请选择" @change="changeValue(3,ruleForm.threeDemand)">
                  <el-option v-for="(item,i) of ruleForm.regions" :key="i" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </div>
            <el-form-item label="主诉" prop="appeal">
              <el-input v-model="ruleForm.appeal" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="正在服用药物" prop="usingDrug">
              <el-input v-model="ruleForm.usingDrug" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="健康状况" prop="healthStatus">
              <el-input v-model="ruleForm.healthStatus" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="诊断" prop="diagnosisResult">
              <el-input v-model="ruleForm.diagnosisResult" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="处理方案" prop="handPlane">
              <el-input v-model="ruleForm.handPlane" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="咨询结果" prop="result">
              <el-radio v-model="ruleForm.result" label="1">未成交</el-radio>
              <el-radio v-model="ruleForm.result" label="2">成交</el-radio>
            </el-form-item>
            <el-form-item v-if="ruleForm.result==='1'" label="未成交原因" prop="loseReason">
              <el-input v-model="ruleForm.loseReason" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="原因补充" prop="reasonMsg">
              <el-input v-model="ruleForm.reasonMsg" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="附件" prop="attachUrl">
              <el-upload :action="uploadUrl" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="fileRemove" :before-remove="handleRemove" :on-success="handleList">
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
            </el-form-item>
            <el-form-item>
              <el-button class="submit" type="primary" @click="submitForm('ruleForm')">提交</el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <el-row :span="12">
          顾客相关资料
        </el-row>
      </el-row>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: "Index",
  props: {
    modalVisibleData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      uploadUrl: window.GLOBAL.FILE_SERVER + "/api/v1/base/attach/data/upload/attach",
      modalShow: false,
      dialogImageUrl: '',
      dialogVisible: false,
      demandData: [], // 添加咨询时主次需求
      ruleForm: {},
      rules: {
        oneDemand: [// 主要需求
          { required: true, message: '请选择主要需求', trigger: 'change' },
        ],
        result: [// 咨询结果
          { required: true, message: '请输入咨询结果', trigger: 'blur' },
        ],
        loseReason: [// 未成交原因
          { required: true, message: '请输入原因', trigger: 'blur' },
        ],
      },
    };
  },
  mounted() {
    this.fetchProduct('doctor/consulting/productList');
    console.log(this.modalVisibleData, "this.ruleForm");
  },
  methods: {
    handleList(response, file, fileList) {
      const list = [];
      fileList.forEach(item => {
        list.push(item.response.data.id);
      });
      this.$message.success('上传成功');
      this.ruleForm.attachUrl = list.join(",");
      // console.log(response, file, fileList, "888888");
    },
    fetchProduct(url) {
      this.$api[url]({
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      }).then(res => {
        if (res.code !== 200) return;
        this.$set(this.ruleForm, 'regions', res.data.result);
      });
    },
    changeValue(number, id) {
      const regions = this.ruleForm.regions;
      if (number === 1 && id) {
        this.dataSome(id, number, regions);
      } else if (number === 2 && id) {
        this.dataSome(id, number, regions);
      } else if (number === 3 && id) {
        this.dataSome(id, number, regions);
      }
    },
    dataSome(id, number, regions) {
      let is = 0;
      const bool = this.demandData.some((item, i) => {
        is = i;
        return this.demandData[i].demandSort === number;
      });
      bool ? this.demandData[is] = this.demandDtos(id, regions, number) : this.demandData.push(this.demandDtos(id, regions, number));
    },
    init() {
      this.modalShow = true;
      this.$nextTick(() => {
        console.log(this.$refs[this.$refs.ruleForm], "00909909");
        // this.$refs[formName].resetFields();
      });
    },
    modalClose() {
      this.modalShow = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api['doctor/consulting/addProduct']({
            ...this.modalVisibleData, ...this.ruleForm,
            'demandDtos': this.demandData,
          }).then(res => {
            if (res.code !== 200) return;
            this.$emit('close');
            this.$emit('success');
            this.$message.success("添加咨询成功");
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    async handleRemove(file, fileList) {
      const id = file.response.data.id;
      const res = await this.$api['doctor/consulting/deleteImage']({ fileId: id });
      if (res.code !== 200) return false;
      this.$message.success('删除成功');
    },
    fileRemove(file, fileList) {
      const list = [];
      fileList.forEach(item => {
        list.push(item.response.data.id);
      });
      this.ruleForm.attachUrl = list.join(",");
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    demandDtos(projectId, regions, number) {
      let obj = {};
      regions.forEach(r => {
        if (projectId === r.id) obj = { "demandSort": number, "projectId": r.id, "projectName": r.name };
      });
      return obj;
    },
  },
};

</script>

<style scoped lang="scss">
.top {
  display: flex;
}
.buttons {
  padding: 2px 4px !important;
  height: 32px;
  margin-left: 10px;
  background: rgb(81, 204, 194);
  color: #fff;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
